import json
import random
import time

from flask import Blueprint, request, jsonify
from application.models.users.user import *
from application.tools.myredis import r
comment_blue = Blueprint("commentblue", __name__)



# 获取评论的分类
@comment_blue.route("/comment", methods=["GET", "POST"])
def comment():
    if request.method == "GET":
        # 获取分类
        types = CommentType.query.all()
        tlist = [{
            "id": i.id,
            "name": i.name
        } for i in types]
        # 获取默认分类吓得评论
        # 查询此分类下面的评论及回复
        sql = "select u.url,u.nikename,u.level,c.title,c.tcount,c.tviews,c.id from comment as c inner join user as u on c.userid=u.id where c.pid=0"
        res = db.session.execute(sql)
        comments = res.fetchall()
        commentslist = []
        for i in comments:
            commentslist.append(
                {'url': i[0], 'nikename': i[1], 'level': i[2], 'title': i[3], 'tcount': i[4], 'tviews': i[5], "id": i[6]})
        qqq = "select u.nikename,u.level,c.create_time from user as u inner join comment as c on u.id = c.userid where c.is_last=1"
        www = db.session.execute(qqq)
        ucomment = www.fetchall()
        ucommentslist = []
        for i in ucomment:
            ctime = i['create_time']
            hour = int((int(time.time()) - int(time.mktime(ctime.timetuple()))) / 3600)
            ucommentslist.append({'nikename': i[0], 'level': i[1], 'times': hour})
        return jsonify({'code': 200, "tlist": tlist, "commentslist": commentslist, "ucommentslist": ucommentslist})
    else:

        # 获取参数 userid comment 评价id
        data = json.loads(request.data)
        userid = data.get('userid')
        comment = data.get('comment')
        # 被评价的id
        cid = data.get('cid')
        # 分类id
        typeid = data.get('typeid')
        # 添加回复的信息
        addcomment = Comment(title=comment,is_last=1, userid=userid,typepid=typeid, pid=cid)

        db.session.add(addcomment)
        db.session.commit()
        return jsonify({"code": 200})

# 获取评论下所有的评论
@comment_blue.route("/commentall")
def commentall():
    # 获取评论的id和pid
    cid = request.args.get("id")

    # 通过评论id获取过来这条评论的信息
    sql = " select u.url,u.nikename,u.level,c.title,c.tcount,c.tviews,c.id,c.create_time from comment as c left join user as u on c.userid=u.id  where c.id=%s"%cid
    res= db.session.execute(sql)
    comments = res.fetchall()
    clist = []
    for i in comments:
        # 获取来的时间
        ctime = i['create_time']
        # 时间戳转换
        hour = int((int(time.time()) - int(time.mktime(ctime.timetuple()))) / 3600)
        clist.append(
        {'url': i[0], 'nikename': i[1], 'level': i[2], 'title': i[3], 'tcount': i[4], 'tviews': i[5], "id": i[6],"time": hour}
        )

    sql = " select u.url,u.nikename,u.level,c.id,c.create_time from comment as c left join user as u on c.userid=u.id  where c.pid=%s"%cid
    res= db.session.execute(sql)
    comments = res.fetchall()
    ulist = []
    for i in comments:
        # 获取来的时间
        ctime = i['create_time']
        # 时间戳转换
        hour = int((int(time.time()) - int(time.mktime(ctime.timetuple()))) / 3600)
        ulist.append(
        {'url': i[0], 'nikename': i[1], 'level': i[2], "id": i[3],"time": hour}
        )
    return jsonify({
        "ulist": ulist,
        "clist": clist,
        "code": 200
    })






